package br.ufc.storm.core;

import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.Scanner;

import org.apache.axis2.AxisFault;

import br.ufc.storm.core.CoreServicesStub.AddInstantiationType;
import br.ufc.storm.core.CoreServicesStub.AddInstantiationTypeResponse;
import br.ufc.storm.core.CoreServicesStub.EchoResponse;
import br.ufc.storm.core.CoreServicesStub.ListResponse;
import br.ufc.storm.core.CoreServicesStub.RegisterResponse;

public class CoreServiceConsumer {

	public static void main(String [] args){
		
		//if(register("http://lia.ufc.br/~jeffersonc/compteste.xml")){//
		if(registerIT("exe.xml")){
		System.out.println("Deu certo");
		}else{
			System.out.println("Deu errado");
		}
		//System.out.println(testEcho(readFile("component.xml")));
	}
	
	private static String readFile(String pathname) throws IOException {

	    File file = new File(pathname);
	    StringBuilder fileContents = new StringBuilder((int)file.length());
	    Scanner scanner = new Scanner(file);
	    String lineSeparator = System.getProperty("line.separator");
	    try {
	        while(scanner.hasNextLine()) {        
	            fileContents.append(scanner.nextLine() + lineSeparator);
	        }
	        return fileContents.toString();
	    } finally {
	        scanner.close();
	    }
	}
	
	public static String listComponents(){
		CoreServicesStub stub = null;
		try {
			stub = new CoreServicesStub();
		} catch (AxisFault e1) {
			e1.printStackTrace();
		}
		//Cria a requisicao para o servico
		CoreServicesStub.List request;
		request = new CoreServicesStub.List();
		//Invoca o servico
		ListResponse response = null;
		try {
			response = stub.list(request);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return response.get_return();
	}
	
	public static String testEcho(String str){
		CoreServicesStub stub = null;
		try {
			stub = new CoreServicesStub();
		} catch (AxisFault e1) {
			e1.printStackTrace();
		}
		//Cria a requisicao para o servico
		CoreServicesStub.Echo request;
		request = new CoreServicesStub.Echo();
		request.setValue(str);
		//Invoca o servico
		EchoResponse response = null;
		try {
			response = stub.echo(request);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return response.get_return();
	}
	
	
	
	public static boolean register(String str){
		try {
			str = readFile(str);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		CoreServicesStub stub = null;
		try {
			stub = new CoreServicesStub();
		} catch (AxisFault e1) {
			e1.printStackTrace();
		}
		//Cria a requisicao para o servico
		CoreServicesStub.Register request;
		request = new CoreServicesStub.Register();
		request.setCmp(str);
		//Invoca o servico
		RegisterResponse response = null;
		try {
			response = stub.register(request);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return response.get_return();
	}
	
	public static boolean registerIT(String str){
		try {
			str = readFile(str);
		} catch (IOException e2) {
			// TODO Auto-generated catch block
			e2.printStackTrace();
		}
		CoreServicesStub stub = null;
		try {
			stub = new CoreServicesStub();
		} catch (AxisFault e1) {
			e1.printStackTrace();
		}
		//Cria a requisicao para o servico
		CoreServicesStub.AddInstantiationType request;
		request = new CoreServicesStub.AddInstantiationType();
		request.setCmp(str);
		//Invoca o servico
		AddInstantiationTypeResponse response = null;
		try {
			response = stub.addInstantiationType(request);
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return response.get_return();
	}
}



